#! /usr/bin/env python

# StationData class
#
# Copyright (C) 2011-2013 Christian T. Steigies <steigies@physik.uni-kiel.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Christian T. Steigies <steigies@physik.uni-kiel.de>"
__license__ = "GPL License"

## --------------------------------------------------------------------------

#import os
import sys
#import datetime
#import calendar
#import MySQLdb # python-mysqldb

#from numpy import array
#http://docs.python.org/library/sqlite3.html
# SpaceWx uses CentOS5 with Python2.4 and pysqlite2
try:
    from pysqlite2 import dbapi2 as sqlite3
except ImportError:
    try:
        from sqlite3 import dbapi2 as sqlite3
    except ImportError:
        print "SQLite module not found"
        sys.exit(1)

#from nmdb.tools.datetool import str2datetime
from nmdb.nmdb.sqlite import SqlData
from nmdb.nmdb.sqlite_format import sql_format, sql_create, sql_insert, sql_select, sql_print

#__all__ = ["", ""]


## --------------------------------------------------------------------------
#class StationData(object):
class StationData(SqlData): # merged in SqlData?
    def __init__(self, name, year, month):

        self._this_station = name
        #self._station = []
        self._station = name
        self._data = []
        self._dict = {}
        self._year = year  # this is not used at all?
        self._month = month  # this is not used at all?

        SqlData.__init__(self, ':memory:')
        #self.__conn = sqlite3.connect(':memory:')
        #self._cursor = self.__conn.cursor()
        # Create table
        #self._cursor.execute('''create table data (datetime timestamp, ucorr real, pressure real, corr real, ceff real)''')
        self._cursor.execute(sql_create)
        self.__conn.commit()

    def station(self):
        return self._station

    def this_station(self):
        return self._this_station


    #def data(self, timestamp = datetime.datetime(1,1,1)):
    #    return self._data

    #def dict(self, timestamp = datetime.datetime(1,1,1)):
    #    return self._dict


## --------------------------------------------------------------------------
def check_data(data, cr_min=1., cr_max=10000.):
    """check data for valid range, return None if invalid or out of range"""

    #print "check_data:", data
    if data is None:
        checked = None
    elif data == '':
        checked = None
    elif data == 0:
        checked = None
    elif data < cr_min:
        print "Countrate: %f out of range (%f - %f)" % (data, cr_min, cr_max)
        checked = None
        #raise ValueError #"countrate too low"
    elif data > cr_max:
        print "Countrate: %f out of range (%f - %f)" % (data, cr_min, cr_max)
        checked = None
        #raise ValueError #"countrate too high"
    else:
        checked = data
        #checked = str("%.2f"%(data))

    return checked


## --------------------------------------------------------------------------
def check_pressure(pressure, p_min=100., p_max=1500.):
    """check pressure for valid range, return None if invalid or out of range"""

    #print "check_pressure:", pressure
    if pressure is None:
        checked = None
    elif pressure == 0:
        checked = None
    elif pressure < p_min:
        print "Pressure: %f out of range (%f - %f)" % (pressure, p_min, p_max)
        checked = None
        #raise ValueError #"pressure too low"
    elif pressure > p_max:
        print "Pressure: %f out of range (%f - %f)" % (pressure, p_min, p_max)
        checked = None
        #raise ValueError #"pressure too high"
    else:
        checked = pressure
        #checked = str("%.2f"%(pressure))

    return checked
## --------------------------------------------------------------------------

if __name__ == "__main__":
    print "This is the StationData module."